﻿using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using StudentManagementSystem.Dtos;
using StudentManagementSystem.Entities;
using StudentManagementSystem.Services;

namespace StudentManagementSystem.Controllers
{
    [ApiController]
    [Route("api/Companies/{companyId}/employees")]
    //[Route("api/[controller]")]
    public class EmployeesController : ControllerBase
    {
        private readonly IMapper _mapper;

        public ICompanyRepository _companyRepository { get; }
        public EmployeesController(ICompanyRepository companyRepository, IMapper mapper)
        {
            _companyRepository = companyRepository
                ?? throw new ArgumentNullException(nameof(companyRepository));

            _mapper = mapper
                ?? throw new ArgumentNullException(nameof(mapper)); ;
        }

        /// <summary>
        /// 获取某个公司的员工，加上性别筛选条件,加上全局搜索
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<EmployeeDto>>>
            GetEmployeesForCompany(Guid companyId, string genderDisplay, string q)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return NotFound();
            }
            var employees = await _companyRepository.GetEmployeesAsync(companyId, genderDisplay, q);
            var employeeDtoS = _mapper.Map<IEnumerable<EmployeeDto>>(employees);
            return Ok(employeeDtoS);
        }

        [HttpGet("{employeeId}", Name = nameof(GetEmployeesForCompany))]
        public async Task<ActionResult<EmployeeDto>>
            GetEmployeesForCompany(Guid companyId, Guid employeeId)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return NotFound();
            }

            var employee = await _companyRepository.GetEmployeeAsync(companyId, employeeId);
            if (employee == null)
            {
                return NotFound();
            }
            var employeeDto = _mapper.Map<EmployeeDto>(employee);
            return Ok(employeeDto);
        }

        [HttpPost]
        public async Task<ActionResult<EmployeeDto>> CreateEmployeeForCompany(Guid companyId, EmployeeAddDto emploee)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return NotFound();
            }
            var entity = _mapper.Map<Employee>(emploee);
            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();
            var returnDto = _mapper.Map<EmployeeDto>(entity);
            return CreatedAtRoute(nameof(GetEmployeesForCompany), new
            {
                companyId = companyId,
                employeeId = returnDto.Id
            }, returnDto);
        }

        [HttpPut(template: "{employeeId}")]
        public async Task<IActionResult> UpdateEmployeeForCompany(
            Guid companyId,
            Guid employeeId, 
            EmployeeUpdateDto employee)
        {
            if(!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return NotFound();
            }
            var employeeEntity = await _companyRepository.GetEmployeeAsync(companyId, employeeId);
            if (employeeEntity == null)
            {
                return NotFound();
            }
            //entity 转化为 updateDto
            //把传进来的employee的值更新到 updateDto
            //把updateDto映射回entity
            _mapper.Map(employee, employeeEntity);
            _companyRepository.UpdateEmployee(employeeEntity);
            await _companyRepository.SaveAsync();
            return NoContent();
        }
    }
}
